(in-package :wsml2reasoner)
;;;; $Id$

(defparameter *last-ontology* nil)

;;; XXX recursive is speculative:  needs a resolver mechanism
(defun load-ontology-recursively (iri)
  (when (eql 'pathname (type-of iri))
    (load-ontology (namestring iri))))

(defun load-ontology (file)
  "Load ontology from FILE returning ONTOLOGY and a LOGICAL-EXPRESSION-FACTORY."
  (declare (ignore logical-expression-factory wsmo-factory))
  (handler-case 
      (let* ((wsmo-manager (cl-user::new 'WSMO4JManager))
 	     (logical-expression-factory 
 	      (#"getLogicalExpressionFactory" wsmo-manager))
 	     (wsmo-factory (#"getWSMOFactory" wsmo-manager))
	     (parser (#"createParser" 'org.wsmo.factory.Factory +java-null+))
	     (input-stream (new-java-stream file))
	     (identifiable (#"parse" parser input-stream)))
	(verbose "Ontology read as ~S" identifiable)
	(setf *last-ontology* identifiable)
	(if (> (length identifiable) 0)
	  (let ((ontology (aref identifiable 0)))
;; XXX unable to check instancof
;;	    (when (jinstance-of-p ontology "com.ontotext.wsmo4j.ontology.OntologyImpl")
;;				  "org.omwg.ontology.Ontology")
	     (return-from load-ontology 
	       (values ontology logical-expression-factory)))
	  (progn 
	    (warn "First element of file '~A' is not an ontology" file)
	    +java-null+)))
    (java-exception (e)
      (warn 
       "Failed to load file '~S' as ontology because of exception ~
  ~A"
       file e))
      (java-throwable (e)
	(warn "Failed to load file '~S' as ontology because of ~
  throwable ~S~" file e)))
  +java-null+)
